const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: -1,
    value: '',
    actionText: '查询',
    dicChildList: [],
    total: 0, //分页总数
    page: 1, //分页记录数
    pageSize: 10, //分页大小
    onRefresh: true,
    showTextAndTitleWithInput: false,
    showTextAndTitleWithAdd: false,
    item: {},
    dialogName: "新增",
    dialogValue: "",
    updateId: ""
  },

  //新增弹窗数据改变
  inputDicName: function(e){
    this.setData({
      dialogValue: e.detail.value
    })
  },

  //新增弹窗点击确定
  confirmAddDic: function () {
    let that = this;
    let updateId = that.data.updateId;
    let url = 'dic/addDic';
    let paramsData = {};
    if (updateId == undefined || updateId == null || updateId == '') {
      paramsData = {
        'parentId': that.data.id,
        'dicName': that.data.dialogValue
      }
    } else {
      url = "dic/updateDic";
      paramsData = {
        'parentId': that.data.id,
        'dicName': that.data.dialogValue,
        'id': updateId
      }
    }
    let obj = {
      method: 'POST',
      showLoading: false,
      url: url,
      data: paramsData
    }
    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        this.setData({
          onRefresh: false,
          showTextAndTitleWithAdd: false
        })
        this.getMyDicFunc();
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
    const {
      dialogKey
    } = this.data;
    this.setData({
      [dialogKey]: false
    });
  },

  //点击删除弹窗的取消
  closeDelDialog: function () {
    this.setData({
      ['showTextAndTitleWithInput']: false
    });
  },

  //打开新增弹窗
  openAddDialog: function (e) {
    this.setData({
      ['showTextAndTitleWithAdd']: true,
      dialogValue: '',
      updateId: '',
      dialogName: "新增"
    });
  },

  //关闭新增弹窗
  closeAddDialog: function () {
    this.setData({
      ['showTextAndTitleWithAdd']: false
    });
  },

  //点击删除弹窗的确定
  confirmDelDic: function (e) {
    let that = this;
    let item = that.data.item;
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'dic/delDic?dicId=' + item.id
    }

    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        that.setData({
          showTextAndTitleWithInput: false,
          item: {},
          onRefresh: false,
          page: 1,
          dicChildList: []
        });
        that.getMyDicFunc();
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },

  //点击了左滑动删除按钮
  onDelete: function (e) {
    let item = e.currentTarget.dataset.entity;
    this.setData({
      ['showTextAndTitleWithInput']: true,
      item: item
    })
  },

  /**
   * 搜索框改变
   */
  changeHandle: function (e) {
    this.setData({
      value: e.detail.value
    })
  },

  /**
   * 搜索框清空
   */
  clearHandle: function (e) {
    console.log('clearHandle' + e.detail.value)
    this.setData({
      value: ''
    });
  },

  /**
   * 搜索框点击查询
   */
  actionHandle: function () {
    this.setData({
      onRefresh: false,
      page: 1
    });
    this.getMyDicFunc();
  },

  /**
   * 分页查询字典列表
   */
  getMyDicFunc: function () {
    let that = this;
    var onRefresh = that.data.onRefresh; //false为重新刷新数据，true为分页累加数据
    let obj = {
      method: 'POST',
      showLoading: false,
      url: 'dic/query/page',
      data: {
        'parentId': that.data.id,
        'dicName': that.data.value,
        'page': that.data.page,
        'size': that.data.pageSize,
        'detailFlag': true,
        'column': 'dicSort',
        'order': 'AES'
      }
    }
    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        var datas = res.data.data.records;
        if (datas != null && datas.length > 0) {
          if (onRefresh) { //false为重新刷新数据，true为分页累加数据
            that.setData({
              dicChildList: that.data.dicChildList.concat(datas),
              page: res.data.data.current,
              total: res.data.data.pages,
              dialogValue: '',
              updateId: '',
              showTextAndTitleWithInput: false,
              showTextAndTitleWithAdd: false,
            })
          } else {
            that.setData({
              dicChildList: datas,
              page: res.data.data.current,
              total: res.data.data.pages,
              dialogValue: '',
              updateId: '',
              showTextAndTitleWithInput: false,
              showTextAndTitleWithAdd: false,
            })
          }
        } else {
          ui.showToast('没有更多数据了')
        }
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },

  //打开编辑弹窗
  changeName: function (e) {
    let item = e.currentTarget.dataset.entity;
    console.log(item);
    this.setData({
      dialogValue: item.dicName,
      updateId: item.id,
      showTextAndTitleWithAdd: true,
      dialogName: "编辑"
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id
    })
    this.getMyDicFunc();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.getMyDicFunc();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})